package net.ocps.tchs.scarymazegame;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;

import net.ocps.tchs.scarymazegame.splash.splash;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;  



public class scarymazegame extends Game implements Screen{
	public static OrthographicCamera camera;
	//	private SpriteBatch batch;
	private SpriteBatch batch;

	private Texture texture;

	private Sprite sprite;

	private BitmapFont font;

	private int ZOOM_LENGTH = 300;
	private int ZoomScare = ZOOM_LENGTH + 100;
	private int ZoomSpeed = 9;
	private int CircleX =  0;
	private int CircleY =  0;
	public static String mode = "Splash";

	boolean done = false;

	public static ArrayList<tile> tiles = new ArrayList<tile>();
	public static ArrayList<EndTile> EndTiles = new ArrayList<EndTile>();

	String MapFile;

	ShapeRenderer shape;

	static float Xscaler = 1;
	static float Yscaler = 1;
	private boolean[][] map = new boolean [512][512];
	@Override


	public void create() {	

		setScreen(new splash());
		camera = new OrthographicCamera(800,480);

		batch = new SpriteBatch();

		texture = new Texture(Gdx.files.internal("data/mazeEdited.png"));
		//	Texture.setEnforcePotImages(false);
		texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		//circleImg = new Texture(Gdx.files.internal("data/circle.jpg"));


		TextureRegion region = new TextureRegion(texture, 0, 0, 512, 512);

		shape = new ShapeRenderer();

		sprite = new Sprite(region);
		sprite.setBounds(0,0,512,512);
		sprite.setOrigin(0, 0);
		//	sprite.setPosition(-sprite.getWidth(), -sprite.getHeight());

		font = new BitmapFont();



		//Generate map using file input
		FileHandle file = Gdx.files.internal("data/maps/map1.txt");
		MapFile = file.readString();
		int x = -1,y = -1, k = 0;

		while(k < MapFile.length()){
			char inByte = MapFile.charAt(k);
			if(inByte == '1') {
				tiles.add(new tile(x*32,448-y*32, 32, 32));
			}
			if(inByte == '2') {
				EndTiles.add(new EndTile(x*32,448-y*32, 32, 32));
			}

			if(inByte - '0' < 10 && inByte - '0' > -10) {
				x++;

				System.out.print(inByte);
				if(x > 25) {
					x = -1;
					y++;
					System.out.println();
				}
			}
			k++;
		}
	}

	@Override
	public void dispose() {
		batch.dispose();
		texture.dispose();
		super.dispose();
	}

	@Override
	public void render() {		
		Gdx.gl.glClearColor(1, 1, 1, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

		float degree = -1;

		shape.begin(ShapeType.Circle);
		shape.setColor(Color.GRAY);

		shape.circle(150,150, 100);

		if(Gdx.input.isTouched() && mode.equals("game")) {
			int Xtouch = (int) (Gdx.input.getX()/Xscaler);
			int Ytouch = (int) (Gdx.input.getY()/Yscaler);

			degree = (float) (Math.atan2((330)-Ytouch, (150)-Xtouch)*(180/Math.PI)) + 180;
			degree = 360 - degree;
			degree -= 22.5 - 45;
			if(degree < 0) degree += 360;
			degree = (int) degree / 45;
			degree *= 45;

			

			if(degree >90 && degree < 270) CircleX-=3;
			if(degree < 90 || degree > 270) CircleX+=3;
			if(degree > 0 && degree < 180) CircleY+=3;
			if(degree > 180 && degree < 360) CircleY-=3;

		
		}

		if(degree != -1) {
			int xRad = 150+(int) (Math.cos(Math.toRadians(degree))*80);
			int yRad = 150+(int) (Math.sin(Math.toRadians(degree))*80);
			shape.circle(xRad, yRad, 20);
		}
		else {
			shape.circle(150, 150, 20);
		}
		shape.end();




		if(mode.equals("Splash") && splash.done) {
			mode = "Menu";
			setScreen(new Menu());
		}

		if(mode.equals("Game")) {
			mode = "game";
			setScreen(new scarymazegame());
		}

		if(Gdx.input.isKeyPressed(Keys.DPAD_LEFT)) CircleX-=3;
		if(Gdx.input.isKeyPressed(Keys.DPAD_RIGHT)) CircleX+=3;
		if(Gdx.input.isKeyPressed(Keys.DPAD_UP)) CircleY+=3;
		if(Gdx.input.isKeyPressed(Keys.DPAD_DOWN)) CircleY-=3;

		if(done) {
			CircleX = (int) (Math.random()*800);
			CircleY = (int) (Math.random()*480);
		}

		camera.update();

		batch.setProjectionMatrix(camera.combined);

		if(ZoomScare >= ZOOM_LENGTH) {
			ZoomScare = -1;
			CircleX = 15;
			CircleY = 465;
		}

		if(true != false) {
			batch.begin();
			texture = new Texture(Gdx.files.internal("data/mazeEdited.png"));
			//	Texture.setEnforcePotImages(false);
			texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
			//circleImg = new Texture(Gdx.files.internal("data/circle.jpg"));

			TextureRegion region = new TextureRegion(texture, 0, 0, 512, 512);

			sprite = new Sprite(region);
			sprite.setBounds(0,0,512,512);
			sprite.setOrigin(0, 0);

			sprite.setPosition(-256,-256);
			//	sprite.draw(batch);
			texture = new Texture(Gdx.files.internal("data/circleblack.png"));
			texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
			//TextureRegion region = new TextureRegion(texture, 0, 0, 64, 64);
			sprite = new Sprite(texture);

			sprite.setBounds(0,0,16*Xscaler,16*Yscaler);
			sprite.setOrigin(-50, 50);

			sprite.setPosition((int)CircleX,(int) CircleY);

			sprite.draw(batch);

			shape.begin(ShapeType.FilledCircle);
			shape.setColor(Color.BLACK);
			shape.filledCircle(CircleX*scarymazegame.Xscaler, CircleY*scarymazegame.Yscaler, 10);
			shape.end();

			shape.begin(ShapeType.FilledRectangle);
			shape.setColor(Color.BLUE);

			for(int i = 0; i < tiles.size(); i++) {
				//		System.out.println("x: " + tiles.get(i).x*Xscaler + " y: " + tiles.get(i).y*Yscaler + " w: " + tiles.get(i).w*Xscaler +" h: "+tiles.get(i).h*Yscaler);
				shape.filledRect(tiles.get(i).x*scarymazegame.Xscaler,tiles.get(i).y*scarymazegame.Yscaler, tiles.get(i).w*scarymazegame.Xscaler,tiles.get(i).h*scarymazegame.Yscaler);
				if(CircleX > tiles.get(i).x && CircleX < tiles.get(i).x + tiles.get(i).w) {
					if(CircleY > tiles.get(i).y && CircleY < tiles.get(i).y + tiles.get(i).h) {
						ZoomScare = 0;
					}
				}
			}

			shape.setColor(Color.RED);

			for(int i = 0; i < EndTiles.size(); i++) {
				shape.filledRect(EndTiles.get(i).x*scarymazegame.Xscaler,EndTiles.get(i).y*scarymazegame.Yscaler, EndTiles.get(i).w*scarymazegame.Xscaler,EndTiles.get(i).h*scarymazegame.Yscaler);
				//		System.out.println("x: " + tiles.get(i).x*Xscaler + " y: " + tiles.get(i).y*Yscaler + " w: " + tiles.get(i).w*Xscaler +" h: "+tiles.get(i).h*Yscaler);
				if(CircleX > EndTiles.get(i).x && CircleX < EndTiles.get(i).x + EndTiles.get(i).w) {
					if(CircleY > EndTiles.get(i).y && CircleY < EndTiles.get(i).y + EndTiles.get(i).h) {
						System.out.println("DONE");		
						done = true;
					}
				}
			}

			shape.end();
			batch.end();
		}


		if(ZoomScare < ZOOM_LENGTH && ZoomScare != -1 && !done) {
			System.out.println(ZoomScare);
			batch.begin();
			texture = new Texture(Gdx.files.internal("data/splashedit.png"));
			//	Texture.setEnforcePotImages(false);
			texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
			//circleImg = new Texture(Gdx.files.internal("data/circle.jpg"));

			int ZoomPos = 480 - ZoomScare;

			TextureRegion region = new TextureRegion(texture, ZoomScare/2, ZoomScare/2, ZoomPos, ZoomPos);

			sprite = new Sprite(region);
			sprite.setBounds(0,0,800,480);
			sprite.setOrigin(0, 0);


			sprite.setPosition(-400,-240);
			sprite.draw(batch);
			ZoomScare+=ZoomSpeed;
			batch.end();
		}


		super.render();
	}

	@Override
	public void resize(int width, int height) {
		System.out.println("resized: " + width + ", " + height);

		Xscaler = width/camera.viewportWidth;
		Yscaler = height/camera.viewportHeight;
		System.out.println("Scaler: " + Xscaler + ", "+ Yscaler);
		super.resize(width, height);
	}


	@Override
	public void pause() {
		super.pause();
	}

	@Override
	public void resume() {
		super.resume();
	}

	@Override
	public void render(float delta) {
		// TODO Auto-generated method stub

	}

	@Override
	public void show() {
		// TODO Auto-generated method stub

	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub

	}
}
